package topt_12z.network;

import java.util.ArrayList;

import javax.naming.directory.InvalidAttributesException;

import topt_12z.general.Globals;

public class Network {

	ArrayList<ArrayList<Node>> networkGrid = new ArrayList<ArrayList<Node>>();

	public Network() throws Exception {
		this(Globals.NETWORK_SIZE);
	}

	public Network(int NSize) throws Exception {
		if (!(NSize > 0)) {
			throw new InvalidAttributesException("Invalid NSize - " + NSize
					+ ". It has to be larger then 0");
		}
		networkInit(NSize);
		networkAddReferences();
	}

	private void networkInit(int nSize) throws Exception {
		for (int row = 0; row < nSize; row++) {
			ArrayList<Node> rowList = new ArrayList<Node>();
			networkGrid.add(row, rowList);
			for (int col = 0; col < nSize; col++) {
				rowList.add(col, new Node(row, col));
			}
		}
	}

	private void networkAddReferences() {
		for (int row = 0; row < networkGrid.size(); row++) {
			ArrayList<Node> rowList = networkGrid.get(row);
			for (int col = 0; col < rowList.size(); col++) {
				Node tempNode = rowList.get(col);

				// dzielenie modulo 2 na wierszu - jesli wynik =0, to referencja
				// w prawo
				// jesli wynik = 1 to w lewo
				if (row % 2 == 0) {
					if (col == rowList.size() - 1) {
						tempNode.setRowNode(rowList.get(0));// reference to next
															// element
					} else {
						tempNode.setRowNode(rowList.get(col + 1));// reference
																	// to next
																	// element
					}
				} else {
					if (col == 0) {
						tempNode.setRowNode(rowList.get(networkGrid.size() - 1));// reference
																					// to
																					// next
																					// element
					} else {
						tempNode.setRowNode(rowList.get(col - 1));// reference
																	// to next
																	// element
					}

					// dzielenie modulo 2 na kolumnie- jesli wynik =0, to
					// referencja w gore
					// jesli wynik = 1 to w dol
				}
				if (col % 2 == 0) {
					if (row == 0) {
						tempNode.setColumnNode(networkGrid.get(
								networkGrid.size() - 1).get(col));// reference
																	// to 0
																	// element
					} else {
						tempNode.setColumnNode(networkGrid.get(row - 1)
								.get(col));
					}

				} else {
					if (row == networkGrid.size() - 1) {
						tempNode.setColumnNode(networkGrid.get(0).get(col));// reference
																			// to
																			// 0
																			// element
					} else {
						tempNode.setColumnNode(networkGrid.get(row + 1)
								.get(col));
					}
				}
			}
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Network grid:\n");
		for (int row = 0; row < networkGrid.size(); row++) {
			ArrayList<Node> rowList = networkGrid.get(row);
			for (int col = 0; col < rowList.size(); col++) {
				sb.append(rowList.get(col).toString()).append("\t");
			}
			sb.append("\n");
		}

		return sb.toString();
	}

	public String toFullString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Network grid:\n");
		for (int row = 0; row < networkGrid.size(); row++) {
			ArrayList<Node> rowList = networkGrid.get(row);
			for (int col = 0; col < rowList.size(); col++) {
				sb.append(rowList.get(col).toFullString()).append("\n\n");
			}
			sb.append("\n\n");
		}

		return sb.toString();
	}

	public ArrayList<ArrayList<Node>> getNetworkGrid() {
		return networkGrid;
	}



}
